package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.CutAdapter;
import flc.ast.bean.MyCutBean;
import flc.ast.databinding.ActivityPicCutBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class PicCutActivity extends BaseAc<ActivityPicCutBinding> {

    public static String imgPath = "";
    private Bitmap mImgBitmap;
    private CutAdapter cutAdapter;
    private int oldPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_cut;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicCutBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvCutSave.setOnClickListener(this);

        mDataBinding.rvPicCutList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        cutAdapter = new CutAdapter();
        mDataBinding.rvPicCutList.setAdapter(cutAdapter);
        cutAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCutSave:
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        cutAdapter.getItem(oldPosition).setSelect(false);
        cutAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        cutAdapter.getItem(position).setSelect(true);
        cutAdapter.notifyItemChanged(position);
        mDataBinding.cropImgView.setRatioCropRect(mDataBinding.imageViewTouch.getBitmapRect(),
                cutAdapter.getItem(position).getRatio());
    }

    @Override
    protected void initData() {
        //设置图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.imageViewTouch.setImageBitmap(mImgBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        mDataBinding.imageViewTouch.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.imageViewTouch.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
        //裁剪数据
        List<MyCutBean> listCut = new ArrayList<>();
        listCut.add(new MyCutBean(getString(R.string.img_text1), R.drawable.wugb, R.drawable.wudk, -1f, true));
        listCut.add(new MyCutBean(getString(R.string.img_text2), R.drawable.zigb, R.drawable.zidk, -1f, false));
        listCut.add(new MyCutBean(getString(R.string.img_text3), R.drawable.zfxgb2, R.drawable.zfxdk, 1f, false));
        listCut.add(new MyCutBean("4:3", R.drawable.sisgb, R.drawable.sisdk, 4f / 3f, false));
        listCut.add(new MyCutBean("3:4", R.drawable.sansigb2, R.drawable.sansidk, 3f / 4f, false));
        cutAdapter.setList(listCut);
    }

    //裁剪并保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.imageViewTouch.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                //保存图片
                ImageUtils.save2Album(resultBit, Bitmap.CompressFormat.PNG);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.save_suc);
                startActivity(HomeActivity.class);
            }
        });
    }

}